/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.insta.browser.view.tab;

import fr.insta.browser.domain.IHistoryEntry;
import fr.insta.browser.home.SetUpHomePanel;
import fr.insta.browser.service.history.ChangeHistoryListener;
import fr.insta.browser.service.history.IHistoryService;
import fr.insta.browser.service.tab.ITabService;
import fr.insta.browser.view.menu.HistoryPopup;
import fr.insta.browser.view.webview.BaseWebViewListener;
import fr.insta.browser.view.webview.SwingWebView;
import fr.insta.browser.view.webview.WebViewFactory;
import fr.insta.webview.service.ServiceWebObject;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 * <p>ContentTab class.</p>
 *
 * @author Maarek
 * @version $Id: $Id
 */
public class ContentTab extends JPanel implements HeaderViewDelegate, HistoryPopup.HistoryPopupDelegate {

    final private JTabbedPane tabbedPane;
    final private WebViewFactory webViewFactory;
    final private ITabService tabService;
    final private IHistoryService historyService;

    private HeaderView headerView;
    private SwingWebView webView;
    private String homepage;
    private boolean withAddEntry = true;

    /**
     * <p>Constructor for ContentTab.</p>
     *
     * @param historyService a {@link fr.insta.browser.service.history.IHistoryService} object.
     * @param tabService a {@link fr.insta.browser.service.tab.ITabService} object.
     * @param tabbedPane a {@link javax.swing.JTabbedPane} object.
     * @param webViewFactory a {@link fr.insta.browser.view.webview.WebViewFactory} object.
     */
    public ContentTab(IHistoryService historyService, ITabService tabService, JTabbedPane tabbedPane, WebViewFactory webViewFactory) {
    	super();
    	this.historyService = historyService;
    	this.tabService = tabService;
    	this.tabbedPane = tabbedPane;
    	this.webViewFactory = webViewFactory;
    	this.homepage = ServiceWebObject.INSTANCE.unserializeHomepage().get(0).getUrl();
    	initComponents();
    	connectListeners();
        webView.loadURL(homepage);
    }

    /**
     * <p>Constructor for ContentTab.</p>
     *
     * @param historyService a {@link fr.insta.browser.service.history.IHistoryService} object.
     * @param tabService a {@link fr.insta.browser.service.tab.ITabService} object.
     * @param tabbedPane a {@link javax.swing.JTabbedPane} object.
     * @param webViewFactory a {@link fr.insta.browser.view.webview.WebViewFactory} object.
     * @param url2load a {@link java.lang.String} object.
     */
    public ContentTab(IHistoryService historyService, ITabService tabService, JTabbedPane tabbedPane, WebViewFactory webViewFactory, String url2load) {
        super();
        this.historyService = historyService;
        this.tabService = tabService;
        this.tabbedPane = tabbedPane;
        this.webViewFactory = webViewFactory;
        initComponents();
        connectListeners();
        webView.loadURL(url2load);
    }

    private void setTitle(String title) {
        int index = tabbedPane.indexOfComponent(this);
        if (index >= 0) {
            tabbedPane.setTitleAt(index, title);
        }
    }

    private void initComponents() {
        headerView = new HeaderView(tabService.getHistoryService(this));
        webView = webViewFactory.create();
        setLayout(new BorderLayout());
        add(webView.getPanel(), BorderLayout.CENTER);
        add(headerView, BorderLayout.NORTH);

        headerView.setDelegate(this);
    }

    private void connectListeners() {
        headerView.getLocationTextField().setText("joseph");

        webView.addListener(new BaseWebViewListener() {
            @Override
            public void onTitleChange(SwingWebView webView, String oldTitle, String newTitle) {
                setTitle(newTitle);
                if (newTitle != null && !"".equals(newTitle)) {
                    if (withAddEntry) {
                        tabService.getHistoryService(ContentTab.this).addEntry(webView.getUrl(), newTitle, null);
                        historyService.addEntry(webView.getUrl(), newTitle, null);
                    } else {
                        withAddEntry = true;
                    }
                }
            }

            @Override
            public void onLocationChange(SwingWebView webView, String oldLocation, String newLocation) {
                headerView.getLocationTextField().setText(newLocation);
            }
        });

        tabService.getHistoryService(this).addListener(new ChangeHistoryListener() {
            @Override
            public void onChangeCurrentEntry(IHistoryEntry entry) {
                if (!webView.getUrl().equals(entry.getUrl())) {
                    withAddEntry = false;
                    webView.loadURL(entry.getUrl());
                }
            }

            @Override
            public void onAddEntry(IHistoryEntry entry) {
            }
        });
    }

    /** {@inheritDoc} */
    @Override
    public void onClickNext(HeaderView view) {
        tabService.getHistoryService(ContentTab.this).next();
    }

    /** {@inheritDoc} */
    @Override
    public void onClickPrevious(HeaderView view) {
        tabService.getHistoryService(ContentTab.this).prev();
    }

    /** {@inheritDoc} */
    @Override
    public void onClickHome(HeaderView view) {
    	this.homepage = ServiceWebObject.INSTANCE.unserializeHomepage().get(0).getUrl();
        webView.loadURL(homepage);
    }

    /** {@inheritDoc} */
    @Override
    public void onLongClickPrevious(HeaderView view) {
        HistoryPopup popup = new HistoryPopup(tabService.getHistoryService(this).iteratorOfCurrentToBegin(), this);
        popup.show(view.getPrevButton(), view.getPrevButton().getX(), view.getPrevButton().getY());
    }

    /** {@inheritDoc} */
    @Override
    public void onLongClickNext(HeaderView view) {
        HistoryPopup popup = new HistoryPopup(tabService.getHistoryService(this).iteratorOfCurrentToEnd(), this);
        popup.show(view.getPrevButton(), view.getPrevButton().getX(), view.getPrevButton().getY());
    }

    /** {@inheritDoc} */
    @Override
    public void onClickItem(HistoryPopup popup, IHistoryEntry entry, JMenuItem item, ActionEvent e) {
        tabService.getHistoryService(this).moveCurrentTo(entry);
    }

    /** {@inheritDoc} */
    @Override
    public void onLocationTextFieldActionPerformed(HeaderView aThis, ActionEvent evt) {
        webView.loadURL(headerView.getLocationTextField().getText());
    }

    /**
     * <p>getCurrentUrl.</p>
     *
     * @return a {@link java.lang.String} object.
     */
    public String getCurrentUrl() {
        return webView.getUrl();
    }

    /**
     * <p>getCurrentTitle.</p>
     *
     * @return a {@link java.lang.String} object.
     */
    public String getCurrentTitle() {
        return webView.getPageTitle();
    }

    /**
     * <p>Getter for the field <code>webView</code>.</p>
     *
     * @return a {@link fr.insta.browser.view.webview.SwingWebView} object.
     */
    public SwingWebView getWebView() {
        return webView;
    }

    /**
     * <p>getDefaultUrl.</p>
     *
     * @return a {@link java.lang.String} object.
     */
    public String getDefaultUrl() {
        return this.homepage;
    }

    /** {@inheritDoc} */
    @Override
    public void onLongClickHome(HeaderView view)
    {
    	this.homepage = ServiceWebObject.INSTANCE.unserializeHomepage().get(0).getUrl();
        webView.loadURL(homepage);
    }

    /** {@inheritDoc} */
    @Override
    public void onClickRefresh() {
        webView.loadURL(headerView.getLocationTextField().getText());
    }
}
